package com.ittest.web.controller.system;

import com.github.pagehelper.PageInfo;
import com.ittest.domain.system.SysLog;
import com.ittest.utils.FileUtil;
import com.ittest.web.controller.BaseServlet;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;

/*
    日志管理控制层
 */
@WebServlet("/system/sysLog/*")
public class SyslogServlet extends BaseServlet {

    // 查询所有
    // public void list(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
    //     // 默认设置，当前页1
    //     int page = 1;
    //     // 默认设置，每页条数5
    //     int size = 5;
    //     // 判断页面是否获取值，有值，按页面参数进行分页
    //     // 当前页面
    //     if (StringUtils.isNotBlank(request.getParameter("page"))){
    //         page = Integer.parseInt(request.getParameter("page"));
    //     }
    //     // 每页显示数据条数
    //     if (StringUtils.isNotBlank(request.getParameter("size"))){
    //         page = Integer.parseInt(request.getParameter("size"));
    //     }
    //     PageInfo all = sysLogService.findAll(page,size);
    //     request.setAttribute("page",all);
    //     // 跳转/pages/system/syslog/syslog-list.jsp页面
    //     request.getRequestDispatcher("/pages/system/syslog/syslog-list.jsp").forward(request,response);
    // }

    // 模糊查询
    public void list(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        // 获取模糊查询参数
        String timeStr = request.getParameter("timeStr");
        // 默认设置，当前页1
        int page = 1;
        // 默认设置，每页条数10
        int size = 10;
        // 判断页面是否获取值，有值，按页面参数进行分页
        // 当前页面
        if (StringUtils.isNotBlank(request.getParameter("page"))){
            page = Integer.parseInt(request.getParameter("page"));
        }
        // 每页显示数据条数
        if (StringUtils.isNotBlank(request.getParameter("size"))){
            size = Integer.parseInt(request.getParameter("size"));
        }
        // 调用service,查询数据
        PageInfo all = sysLogService.findByTime(page,size,timeStr);
        // 存入request域中
        request.setAttribute("timeStr",timeStr);
        request.setAttribute("page",all);
        request.setAttribute("size",size);
        // 跳转/pages/system/syslog/syslog-list.jsp页面
        request.getRequestDispatcher("/pages/system/syslog/syslog-list.jsp").forward(request,response);
    }

    // 删除方法
    public void delete(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
        // 获取arr
        String arr = request.getParameter("arr");

        // 调用业务层接口
        sysLogService.deleteByIds(arr);

        // 跳回页面list
        response.sendRedirect(request.getContextPath()+"/system/sysLog/list");
    }




    // 导出功能
    // downloadReport
    private void downloadReport(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 设置返回数据类型为文件xlsx类型
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        String fileName = new String("日志数据文件.xlsx".getBytes(),"iso8859-1");    // 页面编码iso8859-1
        response.addHeader("Content-Disposition","attachment;fileName="+fileName);
        //生成报告的文件，然后传递到前端页面
        ByteArrayOutputStream os = sysLogService.getReport();
        //获取产生响应的流对象
        ServletOutputStream sos = response.getOutputStream();
        //将数据从原始的字节流对象中提取出来写入到servlet对应的输出流中
        os.writeTo(sos);
        //将输出流刷新
        sos.flush();
        os.close();
    }
}

